import request from "@/utils/request";
import httpRequest from "@/utils/requestUtil";
import { ActivityPageVO } from "./activity.api";
import { IObject } from "../common/common";

const CONTRACT_DESIGN_BASE_URL = "/business/build";

const BuildAPI = {
  /** 获取分页数据 */
  getPage(queryParams?: BuildPageQuery) {
    return httpRequest.get<PageResult<BuildPageVO[]>>(
      `${CONTRACT_DESIGN_BASE_URL}/loadPage`,
      queryParams
    );
  },

  /** 获取分配给自己的数据 */
  loadOwn(queryParams?: PageQuery) {
    return httpRequest.get<PageResult<BuildPageVO[]>>(
      `${CONTRACT_DESIGN_BASE_URL}/loadOwn`,
      queryParams
    );
  },

  /** 获取合同施工详情 */
  loadDetail(contractId: string) {
    return request<any, BuildDetailVO>({
      url: `${CONTRACT_DESIGN_BASE_URL}/loadByContractId`,
      method: "get",
      params: { contractId },
    });
  },

  /**
   * 派单
   * @param contractId 合同id
   * @param majorUserId 负责人
   * @param userIds 构建人员
   * @returns
   */
  dispatch(data: BuildForm) {
    return request<any, boolean>({
      url: `${CONTRACT_DESIGN_BASE_URL}/dispatch`,
      method: "post",
      data,
    });
  },

  /**
   * 绑定模板（项目负责人）
   * @param contractId 合同id
   * @param majorUserId 负责人
   * @param userIds 构建人员
   * @returns
   */
  bind(params: BuildActivityForm) {
    return request<any, ActivityPageVO>({
      url: `${CONTRACT_DESIGN_BASE_URL}/bindFlowId`,
      method: "get",
      params,
    });
  },

  /**
   * 提交申请
   * @param contractId 合同id
   * @param majorUserId 负责人
   * @param userIds 构建人员
   * @returns
   */
  subApproval(data: BuildActivitySubmitForm) {
    return request<any, ActivityPageVO>({
      url: `${CONTRACT_DESIGN_BASE_URL}/subApproval`,
      method: "post",
      data,
    });
  },

  /**
   * 审批
   */
  approval(data: BuildApproveForm) {
    return request<any, boolean>({
      url: `${CONTRACT_DESIGN_BASE_URL}/approval`,
      method: "post",
      data,
    });
  },
};

export default BuildAPI;

/** 分页查询参数 */
export interface BuildPageQuery extends PageQuery {
  /** 搜索关键字 */
  statuses?: string;
  currentTask?: boolean;
}

/** 分页对象 */
export interface BuildPageVO {
  id?: string;
  contractId?: string;
  contractName?: string;
  majorUserId?: string;
  majorUsername?: string;
  dispatchTime?: string;
  activityId: string;
  flowId: string;
  status: string;
  currentNode?:string;
  currentUsers?:string;
}

/** 项目派单表单 */
export interface BuildForm {
  /** 合同id */
  contractId: string;
  /** 负责人id */
  majorUserId: string;
  /** 构建人员ids */
  userIds?: string[];
}

/** 项目流程模板绑定 */
export interface BuildActivityForm {
  /** 流程id */
  flowId: string;
  /** 合同id */
  contractId: string;
}

/** 项目申请提交表单 */
export interface BuildActivitySubmitForm {
  contractId: string;
  subMark: string;
}

/** 项目详情 */
export interface BuildDetailVO extends BuildPageVO {
  contractName: string;
  activityName: string;
  userIds: { userId: string; username: string }[];
}

/** 项目申请审批表单 */
export interface BuildApproveForm {
  contractId: string;
  pass: boolean;
  remark: string;
}

export enum ContractBuildStatus {
  WAIT_DISPATCH = "WAIT_DISPATCH",
  WAIT_APPROVAL = "WAIT_APPROVAL",
  SUB_APPROVAL = "SUB_APPROVAL",
  ACTIVITY = "ACTIVITY",
  END = "END",
}

export const ContractBuild_Status_Label: IObject = {
  [ContractBuildStatus.WAIT_DISPATCH]: "待派单",
  [ContractBuildStatus.WAIT_APPROVAL]: "待申报",
  [ContractBuildStatus.SUB_APPROVAL]: "审批中",
  [ContractBuildStatus.ACTIVITY]: "施工中",
  [ContractBuildStatus.END]: "已完成",
};
